package br.com.lavitstudio.game;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class file_RW {
	private Block[][] block;
	
	public file_RW() {
		
	}
	
	public file_RW(Block[][] block) {
		this.block = block;
	}
	
	// Map data
	public static boolean writeMAP() {
		//System.out.println("Salvando mundo...");
		File file = new File(Component.mapFile);
		try {
		   BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			for(int y=0;y<Level.block.length;y++) {
				for(int x=0; x<Level.block[0].length; x++) {
					bw.write(x + ","+ y + "," + Level.block[x][y].id[0] + "," + Level.block[x][y].id[1]);
				    bw.newLine();
				}
			}
		   
		   bw.flush();
		   bw.close();
		   //System.out.println("Mundo salvo com sucesso!");
		   return true;
		} catch (IOException e) {
			System.out.println(e);
			//System.err.println("Erro ao salvar mundo.");
			return false;
		}
	}
	
	public boolean readMAP() {
		//System.out.println("Carregando mundo...");
		File file = new File(Component.mapFile);
		try {
		   BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		   String mapLine;
		   
		   while((mapLine = br.readLine()) != null) {
			   String[] coordinates = mapLine.split(",");
			   
			   int x  = Integer.parseInt(coordinates[0]);
			   int y  = Integer.parseInt(coordinates[1]);
			   int tX = Integer.parseInt(coordinates[2]);
			   int tY = Integer.parseInt(coordinates[3]);
   
			   block[x][y].id = Tile.getTile(tX, tY);
		   }
		   br.close();
		   //System.out.println("Mundo carregado com sucesso!");
		   return true;
		} catch (IOException e) {
			System.out.println(e);
			//System.err.println("Erro ao carregar mundo.");
			return false;
		}
	}
	
	// Char data
	public static boolean writeCHAR(int startX, int startY, int playerLife, int playerMana, int playerStamina, int playerWeight) {
		File file = new File(Component.charFile);
		try {
		   BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		   
		   bw.write(startX + "," + startY + "," + playerLife + "," + playerMana + "," + playerStamina + "," + playerWeight);
		   bw.newLine();
		   
		   bw.flush();
		   bw.close();
		   return true;
		} catch (IOException e) {
			System.out.println(e);
			return false;
		}
	}
	
	public boolean readCHAR() {
		File file = new File(Component.charFile);
		try {
		   BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		   String charLine = br.readLine();
		   String[] info = charLine.split(",");
		   
		   Character.startX			= Integer.parseInt(info[0]);
		   Character.startY			= Integer.parseInt(info[1]);
		   Character.playerLife		= Integer.parseInt(info[2]);
		   Character.playerMana		= Integer.parseInt(info[3]);
		   Character.playerStamina	= Integer.parseInt(info[4]);
		   Character.playerWeight	= Integer.parseInt(info[5]);

		   br.close();
		   return true;
		} catch (IOException e) {
			System.out.println(e);
			return false;
		}
	}
}
